open Mocha
open Test_utils

@send external map: (array<'a>, 'a => 'b) => array<'b> = "map"
@send external mapi: (array<'a>, ('a, int) => 'b) => array<'b> = "map"

@val external parseInt: string => int = "parseInt"
@val external parseInt_radix: (string, int) => int = "parseInt"

let f = v =>
  if mod(v, 2) == 0 {
    v => v * v
  } else {
    v => v + v
  }

let v = mapi([1, 2, 3], (a, b) => f(a)(b))

let vv = mapi([1, 2, 3], (a, b) => a + b)

let hh = map(["1", "2", "3"], x => parseInt(x))

let u = () => 3

let vvv = ref(0)
let fff = () => {
  /* No inline */
  Console.log("x")
  Console.log("x")
  incr(vvv)
}

let g = () => fff()
/* will be compiled into 
  var g = function () { fff (0)}
  not {[ var g = fff ]}
*/
let abc = (x, y, z) => {
  Console.log("xx")
  Console.log("yy")
  x + y + z
}

let abc_u = (x, y, z) => abc(x, y, z)
/* cool, it will be compiled into 
{[ var absc_u = abc ]}
*/
let () = g()

describe(__MODULE__, () => {
  test("mapi with function", () => {
    eq(__LOC__, v, [0, 1, 4])
  })

  test("mapi with addition", () => {
    eq(__LOC__, vv, [1, 3, 5])
  })

  test("map with parseInt", () => {
    eq(__LOC__, hh, [1, 2, 3])
  })
})

/* FIXME: */
let bar = fn => fn()
/* let hh0001 = fun%raw a b -> {| a + b|}
 let hh0002 = fun%raw () -> {| console.log ("forgiving arity")|} */
bar(%raw(`function(){console.log("forgiving arity")}`))
